home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / J4FTUT04.ZIP / temp / techtut / tut4 / ptech4.txt
Encoding:
Text File  |  1997-09-10  |  4.7 KB  |  121 lines

  1.  
  2.  ========================================================================  
  3.                    Just4Fun Productions presents:
  4.    
  5.                       Part 4 of: the Techtutor
  6.                        (Topics never covered)  
  7.  
  8.  
  9.                              Collisions
  10.  ======================================================================== 
  11.  
  12. Introduction
  13. ============-
  14.  
  15. This time we'll take about collisions. There are multiple types of 
  16. collisions, but the one we'll talk about here is the collision between 2 
  17. sprites. First of all some explanation about a few words I'm going to use:
  18.  * raw-edge = the edge of the complete shape (rectangle)
  19.  * real-edge = the actual "edge" of the image.
  20. so if the image would be a circle the raw-edge would still be a cube that 
  21. surounds the image (the sprite), and the real-edge would be the circle edges 
  22. it self...
  23.  
  24. ----------------------------------------------------------------
  25. For contacting Just4Fun or me (PeeBee) use the following ways:
  26.  
  27. Internet : http://people.zeelandnet.nl/rpb
  28. Email    : just4fun@zeelandnet.nl
  29.  
  30. SnailMail: P.Bestebroer
  31.      Anthony Edenlaan 28
  32.      4463 JC  Goes (Zld.)
  33.       Holland
  34.  
  35. ICQ      : 2309110 (probably fastest way to contact me)
  36. ----------------------------------------------------------------
  37.  
  38. How to do it
  39. ============-
  40.  
  41. Collision detection in games is one of the most important things to get             
  42. right, because you don't want the player to get hit while there's no             
  43. alien actually touching him... on the other side you don't want the player 
  44. to be able to go "thru" another object. There are a few ways of collision 
  45. detection, but not all of them are very effective in terms of speed, and 
  46. pixel-perfection. A good collision detection would check on all the real-
  47. edge pixels of an image, but that would be very S-L-O-W! Just think about 
  48. it, if you would use 32x32 sprite-images, and you had about 50 sprites on 
  49. screen, you would have to check 51200 pixels, that's allmost a complete 
  50. screen! ofcourse this could be done faster by adding some extra image 
  51. information about where the real-edges are, but it would still be very slow 
  52. to check it completely.
  53.  
  54. Another way of checking is a little bit more math-coordinated, you could 
  55. make some triangles, or maybe even rectangled areas that "surround" the 
  56. sprites real-edge, this is medium-fast, and medium-perfect so it might be 
  57. worth checking into that...BUT we need speed! so,
  58.  
  59. The fast way
  60. ============-
  61.  
  62. A faster way is not as pixel-perfect as should, but close enough to             
  63. fool the player. The fast way would be to check on collision inside of the 
  64. raw-edge only. This is fast enough because you'll only have to check on 4
  65. co"rdinates per sprite. Ofcourse this isn't that perfect because            
  66. you'r image's may not be completely filled to the raw-edge. For a more 
  67. effective working of this idea, you'll have to draw you'r real-edges as 
  68. close to the raw-edges as possible, leaving a minimum of "empty-ness" at the 
  69. edge of the images. Those few bytes that are empty, won't get in the way of 
  70. the gameplay because the player won't see it, and doesn't know about it!
  71. And you could also make the collision-rectangle a bit smaller then the true 
  72. sprite-rectangle (leaving some pixels for player-errors).
  73.  
  74. Examples
  75. ========-
  76.  
  77. No example file is attached, but I'll give a simple collision detection
  78. function here:
  79.  
  80.  {
  81.   Rough check if 2 shapes are colliding. This works faster as a
  82.   pixel by pixel check for a collision, and as long as the shapes don't
  83.   have to much "space" on the edges the collision will be allmost
  84.   perfect.
  85.  
  86.   Expects: X+Y position and the WIDTH + HEIGHT of the first sprite,
  87.            X+Y position and the WIDTH + HEIGHT of the second sprite.
  88.  
  89.   Returns: True if collision has been detected.
  90.  }
  91.  FUNCTION collision(x1,y1,w1,h1, x2,y2,w2,h2 :integer):boolean;
  92.  VAR xd,yd : boolean;
  93.  BEGIN
  94.     ASM
  95.       mov xd,false
  96.       mov yd,false
  97.     END;
  98.     if ((x1 <=x2) and (x1+w1>=x2)) then xd:=true else
  99.       if ((x1 <=x2+w2) and (x1+w1>=x2+w2)) then xd:=true else
  100.         if ((x1 >=x2) and (x1+w1<=x2+w2)) then xd:=true;
  101.     if ((y1 <=y2) and (y1+h1>=y2)) then yd:=true else
  102.       if ((y1 <=y2+h2) and (y1+h1>=y2+h2)) then  yd:=true else
  103.         if ((y1 >=y2) and (y1+h1<=y2+h2)) then yd:=true;
  104.     collision:=xd and yd;
  105.  END;
  106.  
  107.  
  108. Final word
  109. ==========-
  110.  
  111. The example shown above was taken from my SuperFX Engine, it work's            
  112. fine and it looks like a pixel-perfect collision detection. 
  113.  
  114. Well that's it for now, The next topic is brand new, and is also new for on 
  115. the web-site, so keep reading for: 
  116.  
  117. Next Tech topic: Demo recording/playing
  118.  
  119.  
  120. PeeBee - September 10th '97
  121.